package com.bsoft.gol.hcb.dao.accessAuth;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import hcn.base.accessAuth.AcApplyRecordEntity;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.atag.dao.ext.HibernateExtendDAO;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
public abstract class AcApplyRecordDAO extends HibernateExtendDAO<AcApplyRecordEntity> {
    public AcApplyRecordDAO() {
        this.setKeyField("recordId");
    }

    /**
     * 查询医生调阅授权申请记录
     * @param mpiId
     * @param resourceId
     * @param resourceType
     * @param bizType
     * @param applyId
     * @param authType
     * @return
     */
    public AcApplyRecordEntity getAAROrdByutime(final String mpiId, final String resourceId, final String resourceType,
                                                       final String bizType,final String applyId, final String authType) {
        HibernateStatelessResultAction<AcApplyRecordEntity> action = new AbstractHibernateStatelessResultAction<AcApplyRecordEntity>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuilder hsql = new StringBuilder();
                hsql.append("select record_id as recordId,tenant_id as tenantId,auth_id as authId,org_id as orgId,mpi_id as mpiId,resource_id as resourceId," +
                        "resource_type as resourceType,biz_id as bizId,biz_type as bizType,apply_id as applyId,apply_name as applyName," +
                        "apply_type as applyType,apply_state as applyState,auth_type as authType,create_name as createName,create_time as createTime,update_name as updateName,update_time as updateTime ");
                hsql.append(" from ac_apply_record where mpi_id=:mpiId and resource_id=:resourceId and resource_type=:resourceType  and biz_type=:bizType ");
                if (StringUtils.isNotEmpty(applyId)){
                    hsql.append("and apply_id=:applyId ");
                }
//                hsql.append(" and auth_type=:authType and (apply_type=1 or apply_type=3) order by update_time desc");
                hsql.append(" and auth_type=:authType and apply_type=1 order by update_time desc");

                Query sqlQuery = ss.createSQLQuery(hsql.toString());
                sqlQuery.setParameter("mpiId", mpiId);
                sqlQuery.setParameter("resourceId", resourceId);
                sqlQuery.setParameter("resourceType", resourceType);
                sqlQuery.setParameter("bizType", bizType);
                if (StringUtils.isNotEmpty(applyId)){
                    sqlQuery.setParameter("applyId", applyId);
                }
                sqlQuery.setParameter("authType", authType);
                sqlQuery.setMaxResults(1);//每页几条
                sqlQuery.setFirstResult(0);//第几页
                sqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List list = sqlQuery.list();
                if (list == null || list.size() == 0) {
                    setResult(null);
                } else {
                    AcApplyRecordEntity deptDetail = ConversionUtils.convert((Map<String, Object>) list.get(0), AcApplyRecordEntity.class);
                    setResult(deptDetail);
                }

            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

//    @DAOMethod(sql = "select recordId, tenantId, authId, orgId, mpiId, resourceId, resourceType, bizId, bizType, applyId, applyName, " +
//            "applyType, applyState, authType, createName, createTime, updateName, updateTime" +
//            " from AcApplyRecordEntity where mpiId=:mpiId and resourceId=:resourceId and resourceType=:resourceType " +
//            "and bizType=:bizType and applyId=:applyId and applyType=:applyType and authType=:authType order by update_time desc limit 0,1")
//    public abstract List<AcApplyRecordEntity> queryAAROrdByutime(@DAOParam("mpiId") String mpiId, @DAOParam("resourceId") String resourceId,
//                                                               @DAOParam("resourceType") String resourceType, @DAOParam("bizType") String bizType,
//                                                               @DAOParam("applyId") String applyId, @DAOParam("applyType") String applyType, @DAOParam("authType") String authType);

    @DAOMethod(sql = "from AcApplyRecordEntity where mpiId=:mpiId and resourceId=:resourceId and resourceType=:resourceType " +
            "and bizType=:bizType and applyId=:applyId and applyType=:applyType and authType=:authType order by update_time desc")
//            "and bizType=:bizType and applyId=:applyId and applyType=:applyType and authType=:authType order by create_time desc limit 0,1")
    public abstract List<AcApplyRecordEntity> queryAAROrdByctime(@DAOParam("mpiId") String mpiId, @DAOParam("resourceId") String resourceId,
                                                               @DAOParam("resourceType") String resourceType, @DAOParam("bizType") String bizType,
                                                               @DAOParam("applyId") String applyId, @DAOParam("applyType") String applyType, @DAOParam("authType") String authType);

    @DAOMethod(sql = "from AcApplyRecordEntity where mpiId=:mpiId and resourceId=:resourceId and resourceType=:resourceType " +
            "and bizType=:bizType and applyId=:applyId and applyType=:applyType and authType=:authType")
    public abstract AcApplyRecordEntity getAcApplyRecord(@DAOParam("mpiId") String mpiId, @DAOParam("resourceId") String resourceId,
                    @DAOParam("resourceType") String resourceType, @DAOParam("bizType") String bizType,
                    @DAOParam("applyId") String applyId, @DAOParam("applyType") String applyType,@DAOParam("authType") String authType);

    /**
     * 根据授权状态查询授权申请记录
     * @param mpiId
     * @param resourceId
     * @param resourceType
     * @param bizType
     * @param applyId
     * @param applyType
     * @param authType
     * @param applyState
     * @return
     */
    @DAOMethod(sql = "from AcApplyRecordEntity where mpiId=:mpiId and resourceId=:resourceId and resourceType=:resourceType " +
            "and bizType=:bizType and applyId=:applyId and applyType=:applyType and authType=:authType and applyState=:applyState")
    public abstract AcApplyRecordEntity getAARByState(@DAOParam("mpiId") String mpiId, @DAOParam("resourceId") String resourceId,
                    @DAOParam("resourceType") String resourceType, @DAOParam("bizType") String bizType,
                    @DAOParam("applyId") String applyId, @DAOParam("applyType") String applyType,@DAOParam("authType") String authType,
                    @DAOParam("applyState") String applyState);



}
